package Ep02_LinkedList._0203;

import Ep02_LinkedList.ListNode;

/**
 * @author Jimmy
 * @date 2023/3/6 - 0:10
 * 说明: 力扣 0203 删除链表节点  方法二：使用虚拟头结点Dummy head
 */
public class DeleteWithDummy {

    public ListNode removeElements(ListNode head, int val){
        if (head == null){
            return head;
        }

        ListNode dummyHead = new ListNode(-1, head);
        ListNode pre = dummyHead;
        ListNode cur = head;
        while(cur != null){
            if(cur.val == val){
                pre.next = cur.next;
            } else {
                pre = cur;
            }
            cur = cur.next;
        }
        return dummyHead.next;
    }
}
